package com.xupt.ttms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xupt.ttms.mapper.OrderMapper;
import com.xupt.ttms.mapper.TicketMapper;
import com.xupt.ttms.mapper.UserMapper;
import com.xupt.ttms.pojo.Movie;
import com.xupt.ttms.pojo.Order;
import com.xupt.ttms.pojo.Seat;
import com.xupt.ttms.service.OrderService;
import com.xupt.ttms.util.DateUtil;
import com.xupt.ttms.util.TypeCasting;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private TicketMapper ticketMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DqServiceImpl dqService;

    @Override
    public int insertOrder(Order order1, List<Seat> list, String pId) {
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowTime = TypeCasting.StringToDate(dateFormat.format(date));
        order1.setGenerateTime(nowTime);
        //当前30分钟后的时间
        String expire_time = DateUtil.TimeAdd(dateFormat.format(date), "30");
        order1.setExpireTime(TypeCasting.StringToDate(expire_time));
        //插入数据
        int orderAffect = orderMapper.insert(order1);
        int ticketAffect = ticketMapper.updateOrderNo(list, pId, order1.getId());
        Order order = orderMapper.selectByPrimaryKey(order1.getId());
        //获取超时时间并转换为long类型数据
        long exPreTime = order.getExpireTime().getTime();
        dqService.putIntoQueue(order1, exPreTime);
        return orderAffect + ticketAffect;
    }

    @Override
    public int payOrder(Order order1, String username) {
        int result = orderMapper.payOrder(order1) + userMapper.payOrder(order1.getAmount(), username);
        return result;
    }

    @Override
    public int payOrder(Order order1) {
        return orderMapper.payOrder(order1);
    }

    @Override
    public void updateCloseOverTimeOrder(Integer dataId) {
        orderMapper.updateCloseOverTimeOrder(dataId);
        ticketMapper.deleteOrder(dataId);
    }

    /**
     * 查询未支付且未超时的订单信息
     *
     * @return
     */
    @Override
    public List<Order> selectFutureOverTimeOrder() {
        return orderMapper.selectFutureOverTimeOrder();
    }

    @Override
    public Order selectByPrimaryKey(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Order> getOrders(int pageNum, int pageSize, Integer id) {
        Page<Order> orders = PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.searchAllByUserId(id);
        List<Order> order = getOrder(orderList, 0);
        PageInfo<Order> pageInfo = new PageInfo<>(order);
        return pageInfo;
    }

    /**
     * 将获取出来相同订单中的座位进行合并
     * @param orders
     * @param count 递归的次数
     * @return
     */
    public List<Order> getOrder(@NotNull List<Order> orders, int count){
        for (int i = count; i < orders.size()-1; i++) {
            int flag = 0;
            int index = i;
            while (orders.get(i).equals(orders.get(index+1))){
                orders.get(i).getSeats().add(orders.get(index+1).getSeats().get(0));
                index++;
                if (index==orders.size()-1){
                    orders.remove(i + 1);
                    return orders;
                }
                flag++;
            }
            if (flag!=0) {
                orders.remove(i + 1);
            }
            if (count!=0){
                orders.remove(count);
            }
            if (flag==0&&count==0){
                orders.remove(i);
            }
        }
        count++;
        return getOrder(orders,count);
    }

    /**
     * 查询未支付的订单
     *
     * @param orderNo
     * @return
     */
    @Override
    public Order getNoBoughtOrderByOrderNo(String orderNo) {
        Order order = orderMapper.selectAllByStatusAndOrderNo(0, orderNo);
        return order;
    }

    @Override
    public Order getOrderByOrderNo(String outTradeNo) {
        return orderMapper.selectAllByOrderNo(outTradeNo);
    }

    @PostConstruct
    public void findOverTime() {
        //程序加载查询所有的未支付的订单
        for (Order order : selectFutureOverTimeOrder()) {
            Date expireTime = order.getExpireTime();
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date nowTime = TypeCasting.StringToDate(dateFormat.format(date));
            if (expireTime.compareTo(nowTime) <= 0) { //超时时间小于等于现在的时间即超时
                updateCloseOverTimeOrder(order.getId());
            }
        }
    }
}
